const Koa = require('koa')
const Router = require('koa-router')
const static = require('koa-static')
const bodyParser = require('koa-bodyparser');
const axios = require('axios');
const app = new Koa()
app.use(bodyParser())
const router = new Router()
app.use(static(__dirname + '/'))

const conf = require('./conf')
const wechat = require('co-wechat')//微信消息中间件
router.all('/wechat',wechat(conf).middleware(
    async message => {
        console.log('wechat',message)
        return 'hello world!' + message.Content
    }
))

const tokenCache = {
    access_token:'',
    updateTime:Date.now(),
    expires_in:7200
}

let a = 0;

router.get('/aPlus1',async ctx=>{
    a= a+1
    ctx.body = '相加完成'
})


router.get('/getAPlus1',async ctx=>{
    ctx.body = a
})

router.get('/getToken',async ctx=>{
    const wxDomain = `https://api.weixin.qq.com`;
    const path = `/cgi-bin/token`;
    const params = `?grant_type=client_credential&appid=${conf.appid}&secret=${conf.appsecret}`
    const url = `${wxDomain}${path}${params}`
    const res = await axios.get(url)
    Object.assign(tokenCache,res.data,{
        updateTime: Date.now()
    })
    ctx.body = res.data;
})

// router.get('/getFollowers',async ctx=>{
//     const url = `https://api.weixin.qq.com/cgi-bin/user/get?access_token=${tokenCache.access_token}`
//     const res = await axios.get(url)
//     console.log("aaaaaaaaaa")
//     console.log(tokenCache)
//     console.log('getFollows',res.data)
//     ctx.body = res.data;
// })
const {ServerToken} = require('./mongoose')
const WechatAPI= require('co-wechat-api')
const api = new WechatAPI(conf.appid,conf.appsecret,
    async function() {
        return await ServerToken.findOne()
    },
    async function(token) {
        const res = await ServerToken.updateOne({},token,{upsert:true})
    }
    )
// 获取关注者列表
router.get('/getFollowers',async ctx=>{
    let res = await api.getFollowers()
    res = await api.batchGetUsers(res.data.openid,'zh_CN')

    ctx.body = res;
})


app.use(router.routes()); /*启动路由*/
app.use(router.allowedMethods());
app.listen(3000);